From 08e75334563fb26da03bbae1faf538d095b36425 Mon Sep 17 00:00:00 2001 From: psyomn Date: Sun, 22 Nov 2015 19:47:30 -0500 Subject: [PATCH] fix #2125: duplicate binary names will cause a bail * adds duplicate target checks for bins, tests, examples, benches * adds tests to test the above --- src/cargo/util/toml.rs | 35 ++++++++++++++++- tests/test_bad_config.rs | 84 ++++++++++++++++++++++++++++++++++++++++ 2 files changed, 118 insertions(+), 1 deletion(-) diff --git a/src/cargo/util/toml.rs b/src/cargo/util/toml.rs index 48df6031b..e7e910b3c 100644 --- a/src/cargo/util/toml.rs +++ b/src/cargo/util/toml.rs @@ -1,4 +1,4 @@ -use std::collections::HashMap; +use std::collections::{HashMap, HashSet}; use std::default::Default; use std::fmt; use std::fs; @@ -465,6 +465,26 @@ impl TomlManifest { None => inferred_bench_targets(layout) }; + if let Err(e) = unique_names_in_targets(&bins) { + bail!("found duplicate binary name {}, but all binary targets \ + must have a unique name", e); + } + + if let Err(e) = unique_names_in_targets(&examples) { + bail!("found duplicate example name {}, but all binary targets \ + must have a unique name", e); + } + + if let Err(e) = unique_names_in_targets(&benches) { + bail!("found duplicate bench name {}, but all binary targets must \ + have a unique name", e); + } + + if let Err(e) = unique_names_in_targets(&tests) { + bail!("found duplicate test name {}, but all binary targets must \ + have a unique name", e) + } + // processing the custom build script let new_build = project.build.as_ref().map(PathBuf::from); @@ -561,6 +581,19 @@ impl TomlManifest { } } +/// Will check a list of toml targets, and make sure the target names are unique within a vector. +/// If not, the name of the offending binary target is returned. +fn unique_names_in_targets(targets: &[TomlTarget]) -> Result<(), String> { + let values = targets.iter().map(|e| e.name()).collect::>(); + let mut seen = HashSet::new(); + for v in values { + if !seen.insert(v.clone()) { + return Err(v); + } + } + Ok(()) +} + fn validate_library_name(target: &TomlTarget) -> CargoResult<()> { match target.name { Some(ref name) => { diff --git a/tests/test_bad_config.rs b/tests/test_bad_config.rs index cbf84d15c..e1d85ef71 100644 --- a/tests/test_bad_config.rs +++ b/tests/test_bad_config.rs @@ -284,3 +284,87 @@ Cargo.toml:[..] ")); }); + +test!(duplicate_binary_names { + let foo = project("foo") + .file("Cargo.toml", r#" + [package] + name = "qqq" + version = "0.1.0" + authors = ["A "] + + [[bin]] + name = "e" + path = "a.rs" + + [[bin]] + name = "e" + path = "b.rs" + "#) + .file("a.rs", r#"fn main() -> () {}"#) + .file("b.rs", r#"fn main() -> () {}"#); + + assert_that(foo.cargo_process("build"), + execs().with_status(101).with_stderr("\ +failed to parse manifest at `[..]` + +Caused by: + found duplicate binary name e, but all binary targets must have a unique name +")); +}); + +test!(duplicate_example_names { + let foo = project("foo") + .file("Cargo.toml", r#" + [package] + name = "qqq" + version = "0.1.0" + authors = ["A "] + + [[example]] + name = "ex" + path = "examples/ex.rs" + + [[example]] + name = "ex" + path = "examples/ex2.rs" + "#) + .file("examples/ex.rs", r#"fn main () -> () {}"#) + .file("examples/ex2.rs", r#"fn main () -> () {}"#); + + assert_that(foo.cargo_process("build").arg("--example").arg("ex"), + execs().with_status(101).with_stderr("\ +failed to parse manifest at `[..]` + +Caused by: + found duplicate example name ex, but all binary targets must have a unique name +")); +}); + +test!(duplicate_bench_names { + let foo = project("foo") + .file("Cargo.toml", r#" + [package] + name = "qqq" + version = "0.1.0" + authors = ["A "] + + [[bench]] + name = "ex" + path = "benches/ex.rs" + + [[bench]] + name = "ex" + path = "benches/ex2.rs" + "#) + .file("benches/ex.rs", r#"fn main () {}"#) + .file("benches/ex2.rs", r#"fn main () {}"#); + + assert_that(foo.cargo_process("bench"), + execs().with_status(101).with_stderr("\ +failed to parse manifest at `[..]` + +Caused by: + found duplicate bench name ex, but all binary targets must have a unique name +")); +}); -- 2.30.2